package com.zmc.lostfound.search.listener;

import com.zmc.lostfound.search.service.UserSearchService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.zmc.lostfound.common.constant.RabbitMqConstant.USER_DELETE_QUEUE;
import static com.zmc.lostfound.common.constant.RabbitMqConstant.USER_SAVE_QUEUE;

/**
 * 用于监听消息队列中的消息
 *
 * @author ZMC
 */
@Component
public class RabbitUserListener {

    private final UserSearchService userSearchService;

    public RabbitUserListener(UserSearchService userSearchService) {
        this.userSearchService = userSearchService;
    }

    /**
     * 监听user新增/修改的业务
     *
     * @param id 主键id
     */
    @RabbitListener(queues = {USER_SAVE_QUEUE})
    public void listenUserSave(Long id) {
        // 根据id新增或修改ES的user索引库的文档
        userSearchService.saveById(id);
    }

    /**
     * 监听user删除的业务
     *
     * @param id 主键id
     */
    @RabbitListener(queues = {USER_DELETE_QUEUE})
    public void listenUserDelete(Long id) {
        // 根据id删除ES的user索引库的文档
        userSearchService.deleteById(id);
    }

}